package com.songlanyun.common.utils;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.apache.commons.lang3.RandomStringUtils;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

//String 工具类
public class StrUtil {
    //判断字符串是否为空
    public static boolean isEmpty(String str) {
        if (null == str || "".equals(str)) {
            return true;
        }
        return false;
    }


    /**
     * 将List<String>集合 转化为String
     * 如{"aaa","bbb"} To 'aaa','bbb'
     */
    public static String convertListToString(List<String> strlist) {
        StringBuffer sb = new StringBuffer();
        if (CollectionUtils.isNotEmpty(strlist)) {
            for (int i = 0; i < strlist.size(); i++) {
                if (i == 0) {
                    sb.append("'").append(strlist.get(i)).append("'");
                } else {
                    sb.append(",").append("'").append(strlist.get(i)).append("'");
                }
            }
        }
        return sb.toString();
    }

    /**
     * 将List<String>集合 转化为String
     * 如{"aaa","bbb"} To 'aaa','bbb'
     */
    public static String convertListObjToString(List<Object> strlist, String field) {
        StringBuffer sb = new StringBuffer();
        if (CollectionUtils.isNotEmpty(strlist)) {
            for (int i = 0; i < strlist.size(); i++) {
                if (i == 0) {
                    sb.append("'").append(MapUtils.objectToMap(strlist.get(i)).get(field)).append("'");
                } else {
                    sb.append(",").append("'").append(MapUtils.objectToMap(strlist).get(field)).append("'");
                }
            }
        }
        return sb.toString();
    }

    /***
     * 将"1,2,3,4,5..."这种形式的字符串转成List<String> 集合
     * @param strs
     * @return
     * */

    public static List<String> converStringToList(String strs) {
        if (StringUtils.isNotBlank(strs)) {
            String[] idStrs = strs.trim().split(",");
            if (null != idStrs && idStrs.length > 0) {
                List<String> strsList = new ArrayList<String>();
                for (String str : idStrs) {
                    if (StringUtils.isNotBlank(str)) {
                        strsList.add(str.trim());
                    }
                }
                if (strsList.size() > 0) {
                    return strsList;
                }
            }
        }
        return null;
    }

    /**
     * 将"1,2,3,4,5..."这种形式的字符串转成"'1','2','3','4'..."这种形式
     *
     * @param strs
     * @return
     */
    public static String converString(String strs) {
        if (StringUtils.isNotBlank(strs)) {
            String[] idStrs = strs.trim().split(",");
            if (null != idStrs && idStrs.length > 0) {
                StringBuffer sbf = new StringBuffer("");
                for (String str : idStrs) {
                    if (StringUtils.isNotBlank(str)) {
                        sbf.append("'").append(str.trim()).append("'").append(",");
                    }
                }
                if (sbf.length() > 0) {
                    sbf = sbf.deleteCharAt(sbf.length() - 1);
                    return sbf.toString();
                }
            }
        }
        return "";
    }

    //判断list是否为空
    public static boolean isEmpty(List list) {
        if (null == list || list.size() < 1) {
            return true;
        }
        return false;
    }

    //判断Collection是否为空
    public static boolean isEmpty(Collection list) {
        if (null == list || list.size() < 1) {
            return true;
        }
        return false;
    }

    //判断Object是否为空
    public static boolean isEmpty(Object object) {
        if (null == object) {
            return true;
        }
        return false;
    }

    //计算日期+天数并返回string类型时间
    public static String plusDay(int num, String newDate) throws ParseException {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date currdate = format.parse(newDate);
        System.out.println("现在的日期是：" + currdate);
        Calendar ca = Calendar.getInstance();
        ca.add(Calendar.DATE, num);// num为增加的天数，可以改变的
        currdate = ca.getTime();
        String enddate = format.format(currdate);
        return enddate;
    }

    /**
     * 获取时间  格式yyyy-MM-dd HH:mm:ss
     *
     * @return
     */
    public static String getNow() {
        return DateFormatUtils.format(new Date(), "yyyy-MM-dd HH:mm:ss");
    }

    public static String getNow(Date date) {
        if (date == null) {
            return null;
        }
        return DateFormatUtils.format(date, "yyyy-MM-dd HH:mm:ss");
    }

    /**
     * 获取时间  格式yyyy-MM-dd
     *
     * @return
     */
    public static String getNowToday() {
        return DateFormatUtils.format(new Date(), "yyyy-MM-dd");
    }

    /**
     * 获取时间  格式yyyy-MM-dd
     *
     * @return
     */
    public static String getNow(String pattern) {
        return DateFormatUtils.format(new Date(), pattern);
    }

    /**
     * 判断时间先后 time1为新时间为true time2为新为false
     */
    public static boolean timeCompare(String time1, String time2) throws ParseException {
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        Date dateTime1 = df.parse(time1);
        Date dateTime2 = df.parse(time2);
        if (dateTime1.getTime() >= dateTime2.getTime()) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * @return 生成订单号
     */
    public static String getOrderNo() {
        String first = RandomStringUtils.randomNumeric(2);
        String end = RandomStringUtils.randomNumeric(4);
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
        String newDate = sdf.format(new Date());
        return first + newDate + end;
    }

    /**
     * @return 生成审核流水号
     */
    public static String getWithdrawNo() {
        String first = RandomStringUtils.randomNumeric(2);
        String end = RandomStringUtils.randomNumeric(2);
        SimpleDateFormat sdf = new SimpleDateFormat("yyMMddHHmmss");
        String newDate = sdf.format(new Date());
        return "sh" + first + newDate + end;
    }


    /**
     * 计算一个子字符串在父字符串中出现的次数
     *
     * @param fStr 父字符串
     * @param sStr 子字符串
     * @return
     */
    public static int strCount(String fStr, String sStr) {
        int count = 0;

        //记录找到的索引
        int i = 0;
        while (true) {
            if (fStr.indexOf(sStr, i) == -1) {
                //找不到，跳出来
                break;
            } else {
                //找到，计数
                count++;
                //更新查找索引值，继续找
                i = fStr.indexOf(sStr, i) + 1;
            }
        }
//        System.out.println(sStr + "在" + fStr + "中出现的次数:" + count);
        return count;
    }

    /**
     * 将富文本中的文字提取出来
     *
     * @return
     */
    public static String extractingText(String content) {
        String regx = "(<.+?>)|(</.+?>)|(&nbsp)";
        Matcher matcher = Pattern.compile(regx).matcher(content);
        while (matcher.find()) {
            // 替换图片
            content = matcher.replaceAll("").replace(" ", "");
        }
        return content;
    }


    /**
     * 截取 只有20个字 -- 用户微信模板
     *
     * @return
     */
    public static String get20Str(String text) {
        if (text.length() > 20) {
            text = text.substring(0, 17) + "...";
        }
        return text;
    }

    /**
     * 截取 只有规定字数 -- 用户微信模板
     *
     * @return
     */
    public static String getNumStr(String text, int num) {
        if (text.length() > num) {
            text = text.substring(0, num - 3) + "...";
        }
        return text;
    }

    /**
     * 截取补...
     *
     * @return
     */
    public static String truncation(String str, int len) {
        if (str.length() > len) {
            str = str.substring(0, len) + "...";
        }
        return str;
    }


    /**
     * 给字符串的左补0或右补0
     * @param str  要处理的字符串
     * @param length 补0后字符串总长度
     * @param type  1-左补0  2-右补0
     * @return
     */
    public static String addZeroForStr(String str, int length,int type) {
        int strLen = str.length();
        if (strLen < length) {
            while (strLen < length) {
                StringBuffer sb = new StringBuffer();
                if(type==1){
                    // 左补0
                    sb.append("0").append(str);
                }else if(type==2){
                    //右补0
                    sb.append(str).append("0");
                }
                str = sb.toString();
                strLen = str.length();
            }
        }
        return str;
    }
}
